package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Cart;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.service.ICartService;
import com.situ.mall.vo.CartVO;

@Service
public class CartServiceImpl implements ICartService{
	@Autowired
	private CartMapper cartMapper;

	@Override
	public void insert(Cart cart) {
		// 首先根据product_id和user_id看购物车里面有没有
		int count = cartMapper.checkExist(cart.getUserId(), cart.getProductId());
		// 如果已经有，就更新一下数量
		if (count > 0) {
			cartMapper.updateQuantity(cart);
		} else {
			// 如果没有再做插入
			cartMapper.insert(cart);
		}
	}

	@Override
	public List<CartVO> selectByUserId(Integer id) {
		return cartMapper.selectByUserId(id);
	}

	@Override
	public void deleteById(int id) {
		cartMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateCheckedStatus(Integer id, Integer checked) {
		cartMapper.updateCheckedStatus(id, checked);
	}

	@Override
	public List<CartVO> selectByUserIdAndChecked(Integer id) {
		return cartMapper.selectByUserIdAndChecked(id);
	}


}
